import { Component, OnInit, Input } from "@angular/core";
import { HttpService } from "src/app/shared/service/http.service";
import { ModalController } from "@ionic/angular";
import { AppGlobalService } from "src/app/shared/service/app-global.service";

@Component({
  selector: "app-show-phone-modal",
  templateUrl: "./show-phone-modal.component.html",
  styleUrls: ["./show-phone-modal.component.scss"],
})
export class ShowPhoneModalComponent implements OnInit {
  @Input() tagData: any = {};
  isShow = false;
  constructor(
    private http: HttpService,
    private modalCtrl: ModalController,
    private appGlobalService: AppGlobalService
  ) {}

  ngOnInit() {
    const defValue = this.tagData.switch.defaultValue;
    this.isShow = defValue === "0" ? false : true;
  }

  handleRadio() {
    this.isShow = !this.isShow;
  }

  handleClick() {
    const button = this.tagData.button[0].submit;
    const filed = this.tagData.switch.field;
    button.content[filed] = this.isShow === false ? "0" : "1";
    const params = {
      content: button.content,
      id: button.id,
      sessionId: localStorage.getItem("sessionId"),
    };

    this.http.post("application/submit", params, (res) => {
      if (res.success) {
        this.appGlobalService.showToast(res.message, 2000, "middle");

      } else {
        // 请求数据失败
        this.appGlobalService.showToast(res.message, 2000, "middle");
      }
      this.modalCtrl.dismiss();
    });
  }
}
